<?php
/**
 * Created by Dumitru Russu
 * User: developer
 * Date: 9/3/12
 * Time: 12:49 PM
 * To change this template use File | Settings | File Templates.
 */
class Page_Be_Accounts_UserGroups extends Page {
    /**
     * @var Db_Accounts_UserGroups
     */
    protected $tableObject;

    function __construct() {
        $this->actionsTableObject = array(
            'saveAction',
            'getNameAction',
            'deleteAction',
            'saveGroupAction',
            'saveGroupsPermissionsAction',
            'cloneAction');
        $this->classTableObject = 'Db_Accounts_UserGroups';
        $userGroupsId = (int)str_replace('ug_id_', '', post('id'));
        $this->idTableObject = (!empty($userGroupsId) ? $userGroupsId : get('id', 'int'));
        parent::__construct();
    }

    public function defaultAction() {
		if (perm('USER_GROUPS_EDIT')){
        	$this->addBreadcrumb(t(T_FORM_TITLE, 'add'), '?action=add');
		}
        $userGroups = Db_Accounts_UserGroups::getAllUserGroups();
        self::$TPL->assign('userGroups', $userGroups);
        self::$TPL->appendJsFile('admin/users');
        self::$TPL->setBodyTemplate('list_user_groups');
        return array(
            'type' => 'display'
        );
    }

    public function addAction() {
		perm('USER_GROUPS_EDIT', '/accounts/usergroups');
        self::$TPL->setBodyTemplate('add_user_group');
        $this->addBreadcrumb(t(T_FORM_TITLE, 'add'), '?action=add', true);
        return array(
            'type' => 'display'
        );
    }

    public function saveAction() {
		perm('USER_GROUPS_EDIT', '/accounts/usergroups');
        $userGroupsName = post('ug_name');
        $this->message = t(T_FORM_MSG, 'success');
        if(!empty($userGroupsName)) {

            $this->tableObject->ug_name = $userGroupsName;

            if(!$this->tableObject->save()) {
                $this->error = true;
                $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
            }
        } else {
            $this->error = true;
            $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
        }

        return array(
            'type' => 'redirect',
            'content' => '?action=userGroups'
        );
    }

    public function deleteAction() {
		perm('USER_GROUPS_EDIT', '/accounts/usergroups');
        $this->message = t(T_FORM_MSG, 'success');
        if(!empty($this->tableObject->objectArray->id)) {
            if(!Db_Accounts_UserGroups::abstractDelete($this->idTableObject)) {
                $this->error = true;
                $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
            }
        } else {
            $this->error = true;
            $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
        }
        return array(
            'type' => 'redirect',
            'content' => '?action=userGroups'
        );
    }

    public function groupsPermissionsAction() {
		perm('USER_GROUPS_EDIT', '/accounts/usergroups');
        $this->addBreadcrumb(t(T_PAGE_TITLE, 'groups_permissions'), '?action=groupsPermissions', true);
        self::$TPL->appendJsFile('admin/permissions');
        $allUserGroups = Db_Accounts_UserGroups::findAllArray();
        $allPermissions = Db_Accounts_Permissions::findAllArray();
        self::$TPL->assign('allPermissions', $allPermissions);
        self::$TPL->assign('allUsersGroups', $allUserGroups);
        self::$TPL->setBodyTemplate('groups_permissions');
        return array(
            'type' => 'display'
        );
    }

    public function getNameAction() {
		perm('USER_GROUPS_EDIT', '/accounts/usergroups');
        $userGroupId = (int)str_replace('ug_id_', '', get('id'));
        return array(
            'type' => 'string',
            'content' => $this->tableObject->getName($userGroupId)
        );
    }

    public function saveGroupAction() {
		perm('USER_GROUPS_EDIT', '/accounts/usergroups');
        $userGroupName = strtolower(post('value', 'string'));

        if(!empty($this->tableObject->objectArray->id) && !empty($userGroupName)) {
            $this->tableObject->ug_name = $userGroupName;
            if(!$this->tableObject->save()) {
                return array(
                    'type' => 'string',
                    'content' => t(T_USER_GROUPS_TITLE, $this->tableObject->objectArray->ug_name)
                );
            } else {
                return array(
                    'type' => 'string',
                    'content' => t(T_USER_GROUPS_TITLE, $userGroupName)
                );
            }
        } else {
            return false;
        }
    }

	public function cloneAction() {
		perm('USER_GROUPS_EDIT', '/accounts/usergroups');
		$this->message = t(T_FORM_MSG, 'success');
		if(!Db_Accounts_UserGroups::cloneRow($this->tableObject->objectArray->id)) {
			$this->error = true;
			$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
		}

		return array(
			'type' => 'redirect',
			'query' => false
		);
	}

    public function managePermissionsAction() {
		perm('USER_GROUPS_EDIT', '/accounts/usergroups');
        $this->addBreadcrumb(t(T_PAGE_TITLE, 'manage_permissions'), '?action=managePermissions', true);
        $this->addBreadcrumb(t(T_FORM_BUTTON, 'add_group'), '?action=addPermissionGroup');
        self::$TPL->appendJsFile('admin/permissions');
        $allPermissions = Db_Accounts_Permissions::getAllPermissions();
        self::$TPL->assign('allPermissions', $allPermissions);
        self::$TPL->setBodyTemplate('manage_permissions');
        return array(
                'type' => 'display'
            );
    }

    public function addPermissionGroupAction() {
		perm('USER_GROUPS_EDIT', '/accounts/usergroups');
        $this->addBreadcrumb(t(T_PAGE_TITLE, 'manage_permissions'), '?action=managePermissions');
        $this->addBreadcrumb(t(T_FORM_BUTTON, 'add_group'), '?action=addPermissionGroup', true);
        self::$TPL->setBodyTemplate('add_permission_group');
        return array(
            'type' => 'display'
        );
    }

    public function savePermissionAction() {
		perm('USER_GROUPS_EDIT', '/accounts/usergroups');
        $permissionId = (int) str_replace('p_id_', '', post('id'));
        $permissionIdentifier = strtoupper(post('value', 'string'));
        $permissions = new Db_Accounts_Permissions($permissionId);

        if(!empty($permissions->objectArray->id) && !empty($permissionIdentifier)) {
            $permissions->pm_identifier = $permissionIdentifier;
            if(!$permissions->save()) {
                return array(
                    'type' => 'string',
                    'content' => $permissions->objectArray->p_identifier
                );
            } else {
                return array(
                    'type' => 'string',
                    'content' => $permissionIdentifier
                );
            }
        } else {
            $permissionParentId = post('pm_parent_id', 'int');
            $permissionIdentifier = strtoupper(post('pm_identifier', 'string'));
            if(!empty($permissionParentId) && !empty($permissionIdentifier)) {
                $permissions->pm_identifier = $permissionIdentifier;
                $permissions->pm_parent_id = $permissionParentId;
                
                $this->message = t(T_FORM_MSG, 'success');
                if(!$permissions->save()) {
                    $this->error = true;
                }
                return array(
                    'type' => 'redirect',
                    'content' => '?action=managePermissions'
                );
            }            
            return false;
        }
    }

    public function saveGroupsPermissionsAction() {
		perm('USER_GROUPS_EDIT', '/accounts/usergroups');
        $usersGroups = post('ug_id', 'array');
        $this->message = t(T_FORM_MSG, 'success');
        $groupPermissions = array();
        foreach($usersGroups as $groupId => $permissions) {
            foreach($permissions as $permissionId => $value) {
                if($value == 1) {
                    $groupPermissions[$groupId][$permissionId] = $value;
                }
            }
        }

        foreach($groupPermissions as $key => $value) {
            if($key != 0) {
                $this->tableObject->id = $key;
                $this->tableObject->ug_permissions = serialize($value);
                if(!$this->tableObject->save()) {
                    $this->error = true;
                    $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
                }
            }
        }
       return array(
           'type' => 'redirect',
           'query' => false
       );
    }


    public function savePermissionGroupAction() {
		perm('USER_GROUPS_EDIT', '/accounts/usergroups');
        $permissionIdentifier = strtoupper(post('pm_identifier', 'string'));
        
        $this->message = t(T_FORM_MSG, 'success');
        if(!empty($permissionIdentifier)) {
            $permissions = new Db_Accounts_Permissions;
            $permissions->pm_identifier = $permissionIdentifier;

            if(!$permissions->save()) {
                $this->error = true;
                $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
            }
        }
        return array(
            'type' => 'redirect',
            'content' => '?action=managePermissions'
        );
    }




    public function deletePermissionAction() {
		perm('USER_GROUPS_EDIT', '/accounts/usergroups');
        $permissionGroupId = get('groupId', 'int');
        $permissionId = get('id', 'int');
        
        $this->message = t(T_FORM_MSG, 'success');
        if(empty($permissionGroupId)) {
            if(!Db_Accounts_Permissions::abstractDelete($permissionId)) {
                $this->error = true;
                $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
            }
            return array(
                'type' => 'redirect',
                'content' => '?action=managePermissions'
            );
        } else {
            if(!Db_Accounts_Permissions::abstractDelete($permissionGroupId)) {
                $this->error = true;
                $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
            } else {
                if(!Db_Accounts_Permissions::abstractDeletePermissions($permissionGroupId)) {
                    $this->error = true;
                    $this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
                }
            }
            return array(
                'type' => 'redirect',
                'content' => '?action=managePermissions'
            );
        }
    }


}
